package edu.hm.risikomanagement;

import org.achartengine.ChartFactory;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import edu.hm.risikomanagement.library.DatabaseData;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class RisikoUebersicht extends Activity {

	private int counter = 0;
	private int[] colors = { Color.BLUE, Color.CYAN, Color.GRAY, Color.GREEN,
			Color.LTGRAY, Color.MAGENTA, Color.RED, Color.WHITE, Color.YELLOW };

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.risiko_uebersicht);
		final Button btn_Grafische_Darstellung = (Button) findViewById(R.id.btn_Grafische_Darstellung);
		final Button btn_Berechnungen = (Button) findViewById(R.id.btn_Berechnungen);
		final Button btn_Gesamtrisiko = (Button) findViewById(R.id.btn_Gesamtrisiko);
		final Button btn_RisikoUebersichtBack = (Button) findViewById(R.id.btn_RisikoUebersichtBack);
		final Intent intent = ChartFactory.getScatterChartIntent(this, getRisikoDataset(),
				getRisikoRenderer());

		btn_Grafische_Darstellung.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				startActivity(intent);
			}
		});

		btn_Berechnungen.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				
				if (DatabaseData.isNetworkAvailable(getApplicationContext())) {
					Intent i = new Intent(RisikoUebersicht.this,
							Berechnungen.class);
					startActivity(i);
				} else {
					Intent i = new Intent(RisikoUebersicht.this, Error.class);
					i.putExtra("error", getString(R.string.connection_error));
					startActivityForResult(i, 1);
				}
				
			}
		});

		btn_Gesamtrisiko.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				
				if (DatabaseData.isNetworkAvailable(getApplicationContext())) {
					Intent i = new Intent(RisikoUebersicht.this, Gesamtrisiko.class);
					startActivity(i);
				} else {
					Intent i = new Intent(RisikoUebersicht.this, Error.class);
					i.putExtra("error", getString(R.string.connection_error));
					startActivityForResult(i, 1);
				}
				
			}
		});

		btn_RisikoUebersichtBack.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});

	}

	private XYMultipleSeriesDataset getRisikoDataset() {
		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

		String selection = DatabaseData
				.getDataFromDatabase("risikoBewertungen");
		String[] items = selection.split("@@@");

		for (String item : items) {
			String[] elements = item.split("&&&");
			XYSeries series = new XYSeries(" " + elements[0]);
			series.add(Double.valueOf(elements[1]),
					Integer.parseInt(elements[2]));
			dataset.addSeries(series);
			counter++;
		}

		return dataset;

	}

	private XYMultipleSeriesRenderer getRisikoRenderer() {
		XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
		renderer.setAxisTitleTextSize(16);
		renderer.setChartTitleTextSize(20);
		renderer.setLabelsTextSize(15);
		renderer.setLegendTextSize(15);
		renderer.setPointSize(5f);
		renderer.setShowGrid(true);
		renderer.setMargins(new int[] { 20, 30, 15, 0 });
		renderer.setXTitle("Euro");
		renderer.setYTitle("Prozent");
		renderer.setAxesColor(Color.DKGRAY);
		renderer.setLabelsColor(Color.LTGRAY);
		
		// ???
		renderer.setXLabels(12);
	    renderer.setYLabels(10);
	    renderer.setDisplayChartValues(true);
	    renderer.setXLabelsAlign(Align.LEFT);
	    renderer.setYLabelsAlign(Align.LEFT);
		
		XYSeriesRenderer r;
		for (int x = 0; x < counter; x++) {
			r = new XYSeriesRenderer();
			r.setPointStyle(PointStyle.CIRCLE);
			r.setColor(colors[x % 9]);
			r.setFillPoints(true);
			renderer.addSeriesRenderer(r);
		}
		return renderer;
	}

}